import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../../assets/styles.dart';
import '../../../components/easy_toast.dart';
import '../../../utils/color.dart';
import 'controller/edit_userinfo_controller.dart';

class SettingAccountPwdPage extends StatelessWidget {
  const SettingAccountPwdPage({super.key});

  @override
  Widget build(BuildContext context) {
    final bgColor = Styles.color.bgColor;

    void onBackClick() {
      Navigator.pop(context);
    }

    return Scaffold(
      backgroundColor: bgColor,
      appBar: AppBar(
        leading: IconButton(
          iconSize: 20.w,
          icon: const Icon(
            Icons.arrow_back_ios,
            color: Colors.white,
          ),
          onPressed: () => onBackClick(),
        ),
        bottom: PreferredSize(
          preferredSize: const Size.fromHeight(1),
          child: Container(
            decoration: BoxDecoration(
              border: Border(
                  bottom: BorderSide(color: COLOR.hexColor("#4d393549"))),
            ),
          ),
        ),
      ),
      body: GetBuilder<EditUserInfoController>(
          init: EditUserInfoController(),
          builder: (controller) {
            return SettingAccountPwdPageState(controller);
          }),
    );
  }
}

class SettingAccountPwdPageState extends StatefulWidget {
  final EditUserInfoController controller;

  const SettingAccountPwdPageState(this.controller, {super.key});

  @override
  State<StatefulWidget> createState() =>
      _SettingAccountPwdPageState(controller);
}

class _SettingAccountPwdPageState extends State<SettingAccountPwdPageState> {
  final EditUserInfoController controller;
  bool _passwordVisible = true;
  String passordSrc = "assets/mine/img_pwd_hide.png";
  TextEditingController contentController = TextEditingController();
  TextEditingController contentControllerTwo = TextEditingController();

  _SettingAccountPwdPageState(this.controller);

  void onClick(int index) {
    switch (index) {
      case 1:
        setState(() {
          _passwordVisible = !_passwordVisible;
          passordSrc = _passwordVisible
              ? "assets/mine/img_pwd_hide.png"
              : "assets/mine/img_pwd_show.png";
        });
        break;
      case 2:
        if (contentController.text.isEmpty) {
          EasyToast.show('请输入账号名称');
          return;
        }
        if (contentControllerTwo.text.isEmpty) {
          EasyToast.show('请输入密码');
          return;
        }
        controller.postSetAccountPwd(
            contentController.text.trim(), contentControllerTwo.text.trim());
        break;
    }
  }

  @override
  void dispose() {
    contentController.dispose();
    contentControllerTwo.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
        padding: EdgeInsets.only(left: 14.w, top: 57.w, right: 14.w),
        width: double.maxFinite,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              "设置账号密码",
              style: TextStyle(
                fontSize: 20.sp,
                fontWeight: FontWeight.bold,
                color: Colors.white,
              ),
            ),
            Padding(padding: EdgeInsets.only(top: 40.w)),
            TextField(
              controller: contentController,
              style: TextStyle(
                color: Colors.white,
                fontSize: 14.sp,
              ),
              decoration: InputDecoration(
                hintText: '请输入账号名称',
                // helperText: '账号无法修改，建议使用手机号/邮箱等常用信息',
                hintStyle: TextStyle(
                  color: Colors.white60,
                  fontSize: 14.sp,
                ),
                helperStyle: TextStyle(
                  color: Colors.white60,
                  fontSize: 11.sp,
                ),
                focusedBorder: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(4.r),
                  borderSide: const BorderSide(
                    style: BorderStyle.none,
                  ),
                ),
                filled: true,
                fillColor: COLOR.hexColor('#ffffff'),
                border: const UnderlineInputBorder(borderSide: BorderSide.none),
                counter: const SizedBox.shrink(),
              ),
            ),
            Padding(padding: EdgeInsets.only(top: 10.w)),
            Text(
              "账号无法修改，建议使用手机号/邮箱等常用信息",
              style: TextStyle(
                fontSize: 11.sp,
                color: Colors.white60,
              ),
            ),
            Padding(padding: EdgeInsets.only(top: 20.w)),
            Stack(
              children: [
                TextField(
                  controller: contentControllerTwo,
                  obscureText: _passwordVisible,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 14.sp,
                  ),
                  decoration: InputDecoration(
                    hintText: '请输入密码',
                    hintStyle: TextStyle(
                      color: Colors.white60,
                      fontSize: 14.sp,
                    ),
                    helperStyle: TextStyle(
                      color: Colors.white60,
                      fontSize: 11.sp,
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(4.r),
                      borderSide: const BorderSide(
                        style: BorderStyle.none,
                      ),
                    ),
                    filled: true,
                    fillColor: COLOR.hexColor('#ffffff'),
                    border:
                        const UnderlineInputBorder(borderSide: BorderSide.none),
                    counter: const SizedBox.shrink(),
                  ),
                ),
                Positioned(
                  right: 10.w,
                  top: 0.0,
                  bottom: 0.0,
                  child: InkWell(
                    onTap: () => onClick(1),
                    child: Image.asset(
                      passordSrc,
                      width: 26.w,
                      height: 26.w,
                    ),
                  ),
                ),
              ],
            ),
            Padding(padding: EdgeInsets.only(top: 10.w)),
            Text(
              "密码无法自主修改，建议使用常用密码",
              style: TextStyle(
                fontSize: 11.sp,
                color: Colors.white60,
              ),
            ),
            Padding(padding: EdgeInsets.only(top: 40.w)),
            InkWell(
              onTap: () => onClick(2),
              child: Container(
                width: double.maxFinite,
                height: 40.w,
                decoration: BoxDecoration(
                  color: COLOR.hexColor('#eb29c6'),
                  borderRadius: BorderRadius.circular(20),
                ),
                child: Flex(
                  direction: Axis.horizontal,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      "登录",
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: Colors.white,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
